package com.liuzu.esys.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liuzu.esys.common.LayuiPageVo;
import com.liuzu.esys.mapper.PrincipalMapper;
import com.liuzu.esys.pojo.Principal;
import com.liuzu.esys.service.PrincipalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Description :天干物燥，小心火烛
 * @Author : yyh
 * @Date : 2022/07/25 15:04:25
 * @desc
 */
@Service("principalService")
public class PrincipalServiceImpl implements PrincipalService {
    @Autowired
    private PrincipalMapper principalMapper;
    @Override
    public LayuiPageVo<Principal> principalPageData(Map<String, Object> params) {
        // mybatis 的 分页插件
        Integer page = Integer.parseInt(params.get("page")+"");
        Integer limit = Integer.parseInt(params.get("limit")+"");
        PageHelper.startPage(page,limit);
        List<Principal> principals = principalMapper.selectByParams(params);
        // mybatis 的 分页数据结构
        PageInfo<Principal> principalPageInfo = new PageInfo<>(principals);
        //把mybatis 的 分页数据 封装到  layui 的分页数据结构中

        LayuiPageVo<Principal> principalLayuiPageVo = new LayuiPageVo<>();
        principalLayuiPageVo.setCode(0);  // layui 默认 0 是正确的状态码
        principalLayuiPageVo.setMsg("分页列表数据"); // 提示信息
        principalLayuiPageVo.setCount(principalPageInfo.getTotal()); // 总记录数
        principalLayuiPageVo.setData(principalPageInfo.getList());  // 分页的列表数据
        return principalLayuiPageVo;
    }

    @Override
    public int deletePrincipal(Integer id) {
        return principalMapper.deleteByPrimaryKey(id);
    }

//    @Override
//    public int deletePrincipalByIds(String ids) {
//        String[] arrId = ids.split(",");
//        return principalMapper.deletePrincipalByIds(arrId);
//    }

    @Override
    public int updatePrincipal(Principal principal) {

        return principalMapper.updatePrincipal(principal);
    }

    @Override
    public int addPrincipal(Principal principal) {
      int i =  principalMapper.addPrincipal(principal);
      return i;
    }
}
